package cn.tedu.egvehicle02.controller;

import cn.tedu.egvehicle02.base.response.JsonResult;
import cn.tedu.egvehicle02.base.response.StatusCode;
import cn.tedu.egvehicle02.mapper.VehicleMapper;
import cn.tedu.egvehicle02.pojo.dto.VehicleAddParam;
import cn.tedu.egvehicle02.pojo.dto.VehicleListQuery;
import cn.tedu.egvehicle02.pojo.entity.Vehicle;
import cn.tedu.egvehicle02.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@Validated
@RestController
@RequestMapping("/v1/vehicle/")
@Tag(name = "车辆管理模块", description = "车辆管理模块")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody @Validated VehicleAddParam vehicleAddParam, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setUserId(userVO.getId());
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        log.debug("添加车辆：{}",vehicle);
        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.ok();
    }
    @GetMapping("list")
    @Operation(summary = "查询车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand",description = "车辆品牌"),
            @Parameter(name = "license",description = "车牌号"),
            @Parameter(name = "vehicleListQuery",hidden = true)
    })
    public JsonResult list(VehicleListQuery vehicleListQuery, HttpSession session) {
        log.debug("查询车辆列表：{}",vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Long userId = userVO.getId();
        return JsonResult.ok(vehicleMapper.selectVehicleList(vehicleListQuery,userId));

    }

    @PostMapping("del/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult delete(@PathVariable @Range(min = 1l,max = 1000l,message = "车辆id范围错误") Long id, HttpSession session) {
        log.debug("删除车辆：{}",id);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Long userId = userVO.getId();
        int rows = vehicleMapper.deleteVehicle(id,userId);
        if (rows!=1){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        return JsonResult.ok();
    }
}
